package top.xmj.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import java.util.*;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

/**
 *
 * @author 冰咖啡
 * @Date:1/12/2022 下午 19:36
 * @Description: websocket配置
 *
 */

@Configuration
public class WebSocketConfig extends ServerEndpointConfig.Configurator{
    /**
     * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        List<String> secs = request.getHeaders().get("Sec-WebSocket-Protocol");
        System.out.println(secs);
        response.getHeaders().put("Sec-WebSocket-Protocol",secs);
        super.modifyHandshake(sec, request, response);
    }
}
